'use strict';
/**
 * 数据同步
 */
app.controller('DataCtrlNotnet', function($scope, $timeout, Xalert, $ionicScrollDelegate, $filter) {
	
	$scope.userid = mqiApp.getUserId(); //userid
    $scope.currentDate = mqiApp.getLocalDataSavedTime($scope.userid);
    $scope.start = $scope.currentDate.slice(0,10)
    $scope.end = $scope.currentDate.slice(11)
	//分页数据
//	$scope.intermediary = [];
	$scope.tacs = [];
	$scope.paging = []
	$scope.loding = true;

	//获取本地数据
	$scope.datas = angular.fromJson(mqiApp.getAllRoadList($scope.userid, true)).road_list;

	//循环分页数据
				for(var i=0;i<$scope.datas.length;i++){
//					$scope.datass.push($scope.datas[i].unit_list)
					$scope.paging.push($scope.datas[i])
					for(var j=0;j<$scope.datas[i].unit_list.length;j++){
						$scope.datas[i].unit_list[j]['road_code'] = $scope.datas[i].road_code;
						$scope.paging.push($scope.datas[i].unit_list[j])
					}
				}
				console.log($scope.paging)
				$scope.tacs = $scope.paging.slice(0,300)
//                $scope.tacs = $scope.paging
//				console.log($scope.tacs)
//				var n = 100
//				$scope.loadMore = function(){
//					n = n+100
//			 		$scope.tacs = $scope.paging.slice(0,n)
//			 		console.log(n)
//			 		console.log($scope.paging.length)
//			 		if(n > $scope.paging.length){
//			 			$scope.loding = false;
//			 		}
//				}
	
	
	var num = 0
	var basic = 0;
	//下一页
	$scope.lodingtwo = false;
	$scope.next = function(){
		num = num + 300 
		if(num > $scope.paging.length){
			Xalert.loading('已经是最后一页啦');
			num = num - 300 
			return false;
		}
		$scope.lodingtwo = true;
		basic = num * ($(".right-flex").height()+1)
		$scope.tacs = [];
		timers = $timeout(function (){
			$scope.tacs = $scope.paging.slice(num-1,num+300)
			for(var i=0, len=$scope.datas.length; i<len; i++){
				if($scope.tacs[0].road_code == $scope.datas[i].road_code){
					$scope.unitTitle = $scope.datas[i].road_code+' '+$scope.datas[i].road_name+' '+$filter("transfor")($scope.datas[i].start_stake)+'~'+$filter("transfor")($scope.datas[i].end_stake)+' '+$scope.datas[i].length+'km';
				}
			}
			$scope.lodingtwo = false;
		},1);
	}
	//上一页
	$scope.prev = function(){
		num = num - 300;
		if(num < 0){
			Xalert.loading('已经是第一页啦');
			num = num + 300
			return false;
		}
		$scope.lodingtwo = true;
		basic = num * ($(".right-flex").height()+1);
		$scope.tacs = [];
		timers = $timeout(function (){
			$scope.tacs = $scope.paging.slice(num>0?num-1:num,num+300)
			for(var i=0, len=$scope.datas.length; i<len; i++){
				if($scope.tacs[0].road_code == $scope.datas[i].road_code){
					$scope.unitTitle = $scope.datas[i].road_code+' '+$scope.datas[i].road_name+' '+$filter("transfor")($scope.datas[i].start_stake)+'~'+$filter("transfor")($scope.datas[i].end_stake)+' '+$scope.datas[i].length+'km';
				}
			}
			$scope.lodingtwo = false;
		},1);
	}
	
	
	var arr = new Array();
	if($scope.datas){
		//区间默认标题
		$scope.unitTitle = $scope.datas[0].road_code+' '+$scope.datas[0].road_name+' '+$filter("transfor")($scope.datas[0].start_stake)+'~'+$filter("transfor")($scope.datas[0].end_stake)+' '+$scope.datas[0].length+'km';
		//各区间的标题的位置（所在第几行）
		arr = [];
		for (var i = 0, len = $scope.datas.length; i < len; i++) {
			arr.push((i >= 1 ? arr[i -1] : 0) + ($scope.datas[i].unit_list).length + 1);
		}
	}

    $timeout(function () {
        //左右滑动的时候获取滚动条的坐标轴，达到联动的效果，
        //内容
        var rightCons = document.getElementById('right-cons');
        rightCons.onscroll = function(){
              var headHandle = $ionicScrollDelegate.$getByHandle("headContainerHandle");
              var aa = $(".right-cons").scrollLeft();//获取当前滚动条的左右进度
              headHandle.scrollTo(aa,false);
         }

	    //上下滚动的时候，获取新的区间标题
         var ionContent = document.getElementById('ion-content');
         ionContent.onscroll = function(){

         		//var unit_height = $(".right-flex").height()
         		var unit_height = $(".right-flex").height()+1;;

         		//滚动的距离
         		var hh = $("#ion-content").scrollTop() + basic;
         		for (var i = 0, len = arr.length; i < len; i++) {
         			if(hh >= 0 && hh < arr[0] *unit_height){
         				$scope.unitTitle = $scope.datas[0].road_code+' '+$scope.datas[0].road_name+' '+$filter("transfor")($scope.datas[0].start_stake)+'~'+$filter("transfor")($scope.datas[0].end_stake)+' '+$scope.datas[0].length+'km';
         			}else if (hh > arr[i] * unit_height) {
         				$scope.unitTitle = $scope.datas[i+1].road_code+' '+$scope.datas[i+1].road_name+' '+$filter("transfor")($scope.datas[i+1].start_stake)+'~'+$filter("transfor")($scope.datas[i+1].end_stake)+' '+$scope.datas[i+1].length+'km';
           			}
         		}

         		//滑动过快，获取值不及时，加个定时器重新获取一下
         		var timers = null;
         		$timeout.cancel(timers);
         		timers = $timeout(function (){
         		    var cc = $("#ion-content").scrollTop();
         		    if (cc === hh) {
                        for (var i = 0, len = arr.length; i < len; i++) {
                            if(hh >= 0 && hh < arr[0] *unit_height){
                                $scope.unitTitle = $scope.datas[0].road_code+' '+$scope.datas[0].road_name+' '+$filter("transfor")($scope.datas[0].start_stake)+'~'+$filter("transfor")($scope.datas[0].end_stake)+' '+$scope.datas[0].length+'km';
                            }else if (hh > arr[i] * unit_height) {
                                $scope.unitTitle = $scope.datas[i+1].road_code+' '+$scope.datas[i+1].road_name+' '+$filter("transfor")($scope.datas[i+1].start_stake)+'~'+$filter("transfor")($scope.datas[i+1].end_stake)+' '+$scope.datas[i+1].length+'km';

                            }
                        }
         			}
         		},300);
         	}
    }, 1000);

    //顶部禁止滑动
	$scope.scroll = function(){
	  	var headHandle = $ionicScrollDelegate.$getByHandle("headContainerHandle");
	  	var aa = $(".right-cons").scrollLeft();
	  	headHandle.scrollTo(aa,false);
	}

	//返回上一页
	$scope.goback = function(){
		mqiApp.goBack();
	}

});
app.filter("transfor", function() {
	function onZHstr(e){
		var zhz = e;
		var mile = parseFloat(zhz);//转换成字符串
		var s = mile.toFixed(3).toString();//把 Number 四舍五入为3位小数
		var a =  s.split(".");//分割字符串成为字符串数组
		if(null == a || a.length == 0){
			return "";
		}else if(a.length == 1){
			var zs = a[0];
			var w = rs;
			return zs;
		}else if(a.length == 2){
			var zs1 = a[0];
			var xs = a[1];
			var rs = zs1+"+" +xs;
			return rs;
		}
	}
	return onZHstr;
});